package com.rlax.ccdemo.service.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rlax.ccdemo.service.user.dto.UpmsUserLoginDTO;
import com.rlax.ccdemo.service.user.entity.UpmsUser;
import com.rlax.ccdemo.service.user.service.UpmsUserService;
import com.rlax.ccdemo.service.user.vo.UpmsUserVO;
import com.rlax.corebin.boot.web.base.BaseController;
import com.rlax.corebin.core.result.R;
import com.rlax.corebin.jwt.model.AuthInfo;
import com.rlax.corebin.mp.page.PageBuilder;
import com.rlax.corebin.mp.page.PageParam;
import com.rlax.corebin.security.annotation.PreAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户表 控制器
 *
 * @author Rlax
 * @since 2021-03-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Api(value = "用户表", tags = "用户表接口")
@PreAuth(replace = "user:")
public class UpmsUserController extends BaseController {

	private final UpmsUserService upmsUserService;

	/**
	 * 详情
	 */
	@PreAuth("hasAnyPermission('{}detail')")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入upmsUser")
	public R<UpmsUser> detail(UpmsUser upmsUser) {
		UpmsUser detail = upmsUserService.getOne(Wrappers.query(upmsUser));
		return R.success(detail);
	}

	/**
	 * 分页 用户表
	 */
	@PreAuth("hasAnyRole('admin')")
	@PostMapping("/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入upmsUser")
	public R<IPage<UpmsUser>> page(@RequestBody PageParam<UpmsUserVO> pageParam) {
		IPage<UpmsUser> pages = upmsUserService.page(PageBuilder.build(pageParam), Wrappers.query(pageParam.getData()));
        return R.success(pages);
	}

	/**
	 * 新增 用户表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增", notes = "传入upmsUser")
	public R<Boolean> save(@Valid @RequestBody UpmsUserVO upmsUser) {
		return R.status(upmsUserService.save(upmsUser));
	}

	/**
	 * 修改 用户表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改", notes = "传入upmsUser")
	public R update(@Valid @RequestBody UpmsUserVO upmsUser) {
		return R.status(upmsUserService.updateById(upmsUser));
	}

	/**
	 * 新增或修改 用户表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "新增或修改", notes = "传入upmsUser")
	public R submit(@Valid @RequestBody UpmsUser upmsUser) {
		return R.status(upmsUserService.saveOrUpdate(upmsUser));
	}

	/**
	 * 删除 用户表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@RequestBody List<Long> ids) {
		return R.status(upmsUserService.removeByIds(ids));
	}

	/**
	 * 登录
	 */
	@PostMapping("/login")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "登录", notes = "传入loginDTO")
	public R<AuthInfo> login(@RequestBody UpmsUserLoginDTO loginDTO) {
		AuthInfo user = upmsUserService.login(loginDTO);
		return R.success(user);
	}
}
